package cn.HuanG.org.service.impl;

import cn.HuanG.base.service.impl.BaseServiceImpl;
import cn.HuanG.org.domain.Employee;
import cn.HuanG.org.domain.Shop;
import cn.HuanG.org.mapper.EmployeeMapper;
import cn.HuanG.org.mapper.ShopMapper;
import cn.HuanG.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void settlement(Shop shop) {
        //1.校验用户手机号是否注册
        //获取前端传来的phone
        String phone = shop.getAdmin().getPhone();
        //再通过phone来查询数据库有无此用户，
        Employee employee = employeeMapper.findByPhone(phone);
        //判断用户是否是空
        if (employee!=null){
            throw new RuntimeException("该手机号已被注册");
        }else {
            shop.setRegisterTime(new Date());
            shop.setState(0);
            shop.getAdmin().setState(0);
        //补全信息
        shop.setRegisterTime(new Date());
        shop.setState(0);
        shop.getAdmin().setState(0);
        //2.保存用户信息，返回主键
        employeeMapper.save(shop.getAdmin());
        //3.保存店铺信息
        shopMapper.save(shop);
        //管理员对应的店铺id
        shop.getAdmin().setShop_id(shop.getId());
        employeeMapper.update(shop.getAdmin());
        }
    }

}
